﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        private List<ListViewItem> _holidayItems;
        private DataView _dvHolidays;

        private HolidaysGroupBy GroupHolidaysBy
        {
            get
            {
                if (cbHolidaysGroupBy.SelectedValue == null ||
                    cbHolidaysGroupBy.SelectedValue.GetType() != typeof(HolidaysGroupBy))
                {
                    return HolidaysGroupBy.None;
                }
                return (HolidaysGroupBy)cbHolidaysGroupBy.SelectedValue; 
            }
        }

        /// <summary>
        /// id of the _employee whose holidays we need to see. -1 if everybody'pShift
        /// </summary>
        /// <remarks>
        /// this Value is only used if someone is selected (highlighted) On sheetHolidayEmployees datagridview
        /// and cb
        /// </remarks>
        private Int64 HolidaysTab_GetSelectedEmployeeID
        {
            get
            {
                try
                {
                    var datasource = dgridHolidayEmployees.DataSource as List<EmployeeAllowance>;
                    return datasource[dgridHolidayEmployees.SelectedRows[0].Index].ID;
                }
                catch
                {
                    return -1;
                }
            }
        }

        internal void HolidaysTab_SelectEmployee(Int64 pEmpID)
        {
            SwitchToTab(tpHolidays);

            cbShowAll.Checked = false;
            cbShowPending.Checked = true;
            
            var datasource = dgridHolidayEmployees.DataSource as List<EmployeeAllowance>;
            if (datasource == null)
            {
                return;
            }
            var seekedGuy = (datasource.Where(guy => guy.ID == pEmpID)).SingleOrDefault();
            if (seekedGuy == null)
            {
                return;
            }
            foreach (var row in dgridHolidayEmployees.Rows.Cast<DataGridViewRow>().Where(
                row => datasource[row.Index] == seekedGuy))
            {
                row.Selected = true;
            }
        }

        #region combobox cbYear
        private void cbYear_Refresh()
        {
            var currentyear = 0;
            if (cbYear.SelectedValue != null &&
                cbYear.SelectedValue.GetType() == typeof(int))
            {
                currentyear = (int) cbYear.SelectedValue;
            }
            cbYear_Sleep();
            cbYear.DataSource = SQLiteAccess.GetHolidayYears();
            if (currentyear > 0)
            {
                cbYear_SeekSelect(currentyear);
            }
            else
            {
                cbYear.SelectedIndex = 0;
            }
            cbYear_Awake();
        }

        /// <summary>
        /// suspends the SelectedValueChanged event handler
        /// </summary>
        private void cbYear_Sleep()
        {
            var foo = new EventHandler(cbYear_SelectedValueChanged);
            cbYear.SelectedValueChanged -= foo;
        }

        private void cbYear_Awake()
        {
            cbYear.SelectedValueChanged += cbYear_SelectedValueChanged;
        }

        private void cbYear_SeekSelect(int year)
        {
            var _index = cbYear.Items.IndexOf(year);
            if (_index >= 0)
            {
                cbYear.SelectedIndex = _index;
            }
        }
        #endregion

        private bool HolidayEditable()
        {
            if (lvHolidays.SelectedTag == null)
            {
                return false;
            }
            if (Program.Logged.CanEditPastHolidays)
            {
                return true;
            }
            if (!Program.Logged.CanEditHolidays)
            {
                return false;
            }
            /* if we got here, the user is allowed to edit holidays, but not past ones
             * so is the selected Holiday - a past one? */
            return (lvHolidays.SelectedTag.timeStatus > 0); // only if it is ahead            
        }

        /// <summary>
        /// sets up the cbHolidaysGroupBy combobox which controls the lvHolidaysGroupBy grouping _mode
        /// </summary>
        /// <remarks>
        /// User is able to view emps_from_database' holidays On the Holidays tab (tpHolidays tab page)
        /// in the lvHolidays listview, grouped either by person, Time status (past-presence-future),
        /// month - or not at all.
        /// 
        /// This method
        /// * sets the datasource for cbHolidaysGroupBy
        /// * associates it with SetAndAssignGroups method via its SelectedValueChanged eventhandler
        /// </remarks>
        private void cbHolidaysGroupBy_Initialize()
        {
            var groupByOptions = new List<KeyValuePair<string, HolidaysGroupBy>>
                                     {
                                         new KeyValuePair<string, HolidaysGroupBy>(
                                             Resources.HOLIDAYSTATUSGROUP_Employee,
                                             HolidaysGroupBy.ByPerson),
                                         new KeyValuePair<string, HolidaysGroupBy>(
                                             Resources.HOLIDAYSTATUSGROUP_TimeStatus,
                                             HolidaysGroupBy.ByTimestatus),
                                         new KeyValuePair<string, HolidaysGroupBy>(
                                             Resources.HOLIDAYSTATUSGROUP_Month,
                                             HolidaysGroupBy.ByMonth),
                                         new KeyValuePair<string, HolidaysGroupBy>(
                                             Resources.HOLIDAYSTATUSGROUP_None,
                                             HolidaysGroupBy.None)
                                     };

            cbHolidaysGroupBy.SelectedValueChanged += (sender, e) => SetAndAssignGroups();
            cbHolidaysGroupBy.DataSource = groupByOptions;
            cbHolidaysGroupBy.DisplayMember = "Key";
            cbHolidaysGroupBy.ValueMember = "Value";
        }
    }
}
